package com.cysyz.web.modules.base.lock.druidfilter;

import com.alibaba.druid.filter.FilterEventAdapter;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
import com.alibaba.druid.proxy.jdbc.StatementProxy;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.util.JdbcUtils;

import java.util.List;

/**
 * Created by cuiyong on 15/6/25.
 */
public class LockStatusFilter extends FilterEventAdapter {
    @Override
    protected void statementExecuteBefore(StatementProxy statement, String sql) {
        System.out.println(statement);
        System.out.println(sql);
    }

    @Override
    protected void statementPrepareAfter(PreparedStatementProxy statement) {

    }

    public static void main(String[] args) {

        String sql ="select p, s.count as views, (select count(*) from Comments rc where rc.linkedId=p.id and rc.classcode='InfoPublishs') as commentNumber, (select count(*) from CollectIndexs rci where rci.toId=p.id and rci.classcode='InfoPublishs' and rci.type='favorite') as favorite FROM InfoPublishs p,UserScores s, InfoPublishs p where p.id=s.linkedId and p.userInfo.id=s.userInfo.id and s.classCode='InfoPublishs' AND p.status=? ORDER BY p.createtime DESC";

        StringBuffer select = new StringBuffer();
            StringBuffer from = new StringBuffer();
            StringBuffer where = new StringBuffer();

            // parser得到AST
            SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcUtils.MYSQL);
            List<SQLStatement> stmtList = parser.parseStatementList(); //

            // 将AST通过visitor输出
            SQLASTOutputVisitor visitor = SQLUtils.createFormatOutputVisitor(from, stmtList, JdbcUtils.MYSQL);
            SQLASTOutputVisitor whereVisitor = SQLUtils.createFormatOutputVisitor(where, stmtList, JdbcUtils.MYSQL);

            for (SQLStatement stmt : stmtList) {

//       stmt.accept(visitor);
                if(stmt instanceof SQLSelectStatement){
                    SQLSelectStatement sstmt = (SQLSelectStatement)stmt;
                    SQLSelect sqlselect = sstmt.getSelect();
                    SQLSelectQueryBlock query = (SQLSelectQueryBlock)sqlselect.getQuery();

                    query.getFrom().accept(visitor);
                    query.getWhere().accept(whereVisitor);

                }
            }

            System.out.println(from.toString());
            System.out.println(select);
            System.out.println(where);
        }
}
